﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Reflection;

namespace RLZY
{
    public partial class Form_Authority : Form
    {
        public Form_Authority()
        {
            InitializeComponent();
        }
        private static Form_Authority childFromInstanc;
        public static bool istime;
        public static Form_Authority ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new Form_Authority();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }
        BLL.tb_menu bll = new BLL.tb_menu();
        role ro = new role();
        BLL.tb_rolemenu bllrolemenu = new BLL.tb_rolemenu();
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private void Form_Authority_Load(object sender, EventArgs e)
        {
            DataSet ds = this.bll.GetList("fmuid=0");
            this.BindData(ds);
            DataSet ds1 = bll.GetList("fmuid >0");
            this.BindData2(ds1);
            DataSet ds3 = bllrolemenu.GetAllList();
            this.BindData3(ds3);
            this.BindListInfo();
        }
        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = bindingSource1;

        }
        private void BindData2(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            bindingSource2.DataSource = dv;
            this.ultraGrid2.DataSource = bindingSource2;
            //this.ShowConditionalFormatDesignerGrid2(this.ultraGrid2.DisplayLayout.Bands[0].Columns["del"]);
        }
        private void BindData3(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            bindingSource3.DataSource = dv;
            this.ultraGrid3.DataSource = bindingSource3;
            //this.ShowConditionalFormatDesignerGrid2(this.ultraGrid2.DisplayLayout.Bands[0].Columns["del"]);
        }
        private void BindListInfo()
        {

            try
            {


                DataSet ds = ro.Getrole();
                cmbRole.DataSource = ds.Tables[0];
                cmbRole.DisplayMember = "roleid";  //列名1 
                cmbRole.ValueMember = "roleid";    //列名2 
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }
        private void ShowConditionalFormatDesignerGrid2(UltraGridColumn column)
        {
            // Show the ConditionValueAppearanceForm to the user. 
            foreach (UltraGridRow row in this.ultraGrid2.Rows)
            {
                row.Cells["del"].Value = "删除";
                row.Cells["del"].Appearance.BackColor = Color.Gray;


            }
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            this.ultraGrid1.DisplayLayout.Bands[0].Columns.Add("checkbox", "标记");
            e.Layout.Bands[0].Columns["checkbox"].Header.VisiblePosition = 3;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["checkbox"].DataType = typeof(bool);
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["checkbox"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
            e.Layout.Bands[0].Columns["fmuname"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
            ultraGrid1.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.SteelBlue;
            e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
        }

        private void ultraGrid1_ClickCell(object sender, ClickCellEventArgs e)
        {

            try
            {

                int i = this.ultraGrid1.ActiveRow.Index;
                string fumame = this.ultraGrid1.Rows[i].Cells["menuname"].Value.ToString().Trim();
                int id = Convert.ToInt32(this.ultraGrid1.Rows[i].Cells[0].Value.ToString().Trim());
                DataSet ds = ro.GetChrole(fumame);
                this.BindData2(ds);
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                //return;
            }
        }

        private void ultraGrid2_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            this.ultraGrid2.DisplayLayout.Bands[0].Columns.Add("checkbox", "标记");
            //this.ultraGrid2.DisplayLayout.Bands[0].Columns.Add("del", "删除");
            e.Layout.Bands[0].Columns["fmuname"].Hidden = true;

            ultraGrid2.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
            ultraGrid2.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.ForestGreen;

            e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            //e.Layout.Bands[0].Columns["del"].Header.VisiblePosition = 4;
            //this.ultraGrid2.DisplayLayout.Bands[0].Columns["del"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button;
            //this.ultraGrid2.DisplayLayout.Bands[0].Override.ButtonStyle = Infragistics.Win.UIElementButtonStyle.Button3D;
            e.Layout.Bands[0].Columns["checkbox"].Header.VisiblePosition = 3;
            this.ultraGrid2.DisplayLayout.Bands[0].Columns["checkbox"].DataType = typeof(bool);
            this.ultraGrid2.DisplayLayout.Bands[0].Columns["checkbox"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
        }

        private void ultraGrid2_ClickCellButton(object sender, CellEventArgs e)
        {
            try
            {
                DialogResult rs = MessageBox.Show("删除后不可恢复，确定要删除吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (rs == DialogResult.OK)
                {
                    int i = this.ultraGrid2.ActiveRow.Index;
                    int id = Convert.ToInt32(this.ultraGrid2.Rows[i].Cells["ID"].Value.ToString().Trim());

                }
            }
            catch { return; }
        }
        private void btnAddList_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult rs = MessageBox.Show("确定添加?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (rs == DialogResult.OK)
                {

                    int b = 0;
                    for (int i = 0; i < this.ultraGrid1.Rows.Count; i++)
                    {
                        if (Convert.ToBoolean(this.ultraGrid1.Rows[i].Cells["checkbox"].Value) == true)
                        {
                            //string id1 = this.ultraGrid1.Rows[i].Cells["计划号"].Value.ToString().Trim();
                            int roleid = Convert.ToInt32(this.cmbRole.Text.Trim());
                            int menuid = int.Parse(this.ultraGrid1.Rows[i].Cells["id"].Value.ToString().Trim());

                            Model.tb_rolemenu model = new Model.tb_rolemenu();
                            model.roleid = roleid;
                            model.menuid = menuid;
                            if (!ro.isExits(roleid, menuid))
                            {
                                bllrolemenu.Add(model);
                                b++;
                            }
                        }
                    }
                    for (int i = 0; i < this.ultraGrid2.Rows.Count; i++)
                    {
                        if (Convert.ToBoolean(this.ultraGrid2.Rows[i].Cells["checkbox"].Value) == true)
                        {
                            //string id1 = this.ultraGrid1.Rows[i].Cells["计划号"].Value.ToString().Trim();
                            int roleid = Convert.ToInt32(this.cmbRole.Text.Trim());
                            int menuid = int.Parse(this.ultraGrid2.Rows[i].Cells["id"].Value.ToString().Trim());

                            Model.tb_rolemenu model = new Model.tb_rolemenu();
                            model.roleid = roleid;
                            model.menuid = menuid;
                            if (!ro.isExits(roleid, menuid))
                            {
                                bllrolemenu.Add(model);
                                b++;
                            }
                        }
                    }

                    MessageBox.Show("添加完成，已添加" + b + "条菜单");
                    if (b > 0)
                    {
                        DataSet ds = ro.GetIDrole(Convert.ToInt32(this.cmbRole.Text.Trim()));
                        this.BindData3(ds);

                    }
                }
            }

            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }
        private void cmbRole_SelectedIndexChanged(object sender, EventArgs e)
        {
                try
                {
                    if (this.cmbRole.Text.Trim().Length > 0)
                    {
                        DataSet ds = ro.GetIDrole(Convert.ToInt32(this.cmbRole.Text.Trim()));
                        this.BindData3(ds);
                    }
                }
                catch (Exception ex)
                {
                    //LOG.Error(ex);
                }
        }

        private void ultraGrid3_ClickCellButton(object sender, CellEventArgs e)
        {
            DialogResult rs = MessageBox.Show("删除后不可恢复，确定要删除吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (rs == DialogResult.OK)
            {
                int i = this.ultraGrid3.ActiveRow.Index;
                int id = Convert.ToInt32(this.ultraGrid3.Rows[i].Cells["id"].Value.ToString().Trim());
                bool ok = bllrolemenu.Delete(id);
                if (ok)
                {
                    DataSet ds = ro.GetIDrole(Convert.ToInt32(this.cmbRole.Text.Trim()));
                    this.BindData3(ds);
                }
            }
        }

        private void ultraGrid3_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            this.ultraGrid3.DisplayLayout.Bands[0].Columns.Add("del", "删除");

            ultraGrid3.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
            ultraGrid3.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.ForestGreen;

            e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            e.Layout.Bands[0].Columns["del"].Header.VisiblePosition = 4;

            this.ultraGrid3.DisplayLayout.Bands[0].Columns["del"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button;
            this.ultraGrid3.DisplayLayout.Bands[0].Override.ButtonStyle = Infragistics.Win.UIElementButtonStyle.Button3D;
        }
    }
}
